package handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import data.Angajat;

import data.AngajatiData;
import data.Scoala;

public class LoadData {
	private AngajatiData angajati = new AngajatiData();

	

	public void LoadAngajati() {
		FileInputStream fin;
		ObjectInputStream objectIn;

		Count dimensiune = new Count();
		try {
			fin = new FileInputStream("DateAngajati.dat");
			objectIn = new ObjectInputStream(fin);
			try {
				dimensiune = (Count) objectIn.readObject();
				
			} catch (ClassNotFoundException e) {
			
			}
			for (int i = 0; i < dimensiune.getNumarMeciuri(); i++)
				try {
					Angajat angajat = new Angajat();
					angajat = (Angajat) objectIn.readObject();
					addAngajat(angajat);
					}

				catch (ClassNotFoundException e) {
				
				}
			fin.close();
			objectIn.close();
		} catch (IOException e) {
		
		}
	}

	public void addAngajat(Angajat angajat) {
		boolean isAngajat = false;
		Scoala scoala;
		for (Scoala school : angajati.getListaScoli())

			if (angajat.getNumeScoala() == school.getNumeScoala()) {
				isAngajat = true;
				scoala = school;
				scoala.addAngajat(angajat);
			}
		if (!isAngajat)

		{
			scoala = new Scoala();
			
			scoala.setNumeScoala(angajat.getNumeScoala());
			scoala.setSchoolStreet(angajat.getSchoolStreet());
			if(angajat.getSchoolNumber()!=null)
				scoala.setSchoolNumber(angajat.getSchoolNumber());
			scoala.setSchoolTown(angajat.getSchoolTown());
			scoala.setSchoolPrincipal(angajat.getSchoolPrincipal());
			scoala.setSchoolAccountant(angajat.getSchoolAccountant());
			
			scoala.addAngajat(angajat);
			angajati.getListaScoli().add(scoala);
		}

	}

	public AngajatiData getlistaScoli() {
		return angajati;
	}

	public void LoadAngajati(File fisier) {
		FileInputStream fin;
		ObjectInputStream objectIn;

		Count dimensiune = new Count();
		try {
			fin = new FileInputStream(fisier);
			objectIn = new ObjectInputStream(fin);
			try {
				dimensiune = (Count) objectIn.readObject();
				System.out.println(dimensiune.getNumarMeciuri());
			} catch (ClassNotFoundException e) {
				System.out.println("1Datele nu au putut fi incarcate");
			}
			for (int i = 0; i < dimensiune.getNumarMeciuri(); i++)
				try {
					Angajat angajat = new Angajat();
					angajat = (Angajat) objectIn.readObject();
					addAngajat(angajat);
				}

				catch (ClassNotFoundException e) {
					System.out.println("2Datele nu au putut fi incarcate");
				}
			fin.close();
			objectIn.close();
		} catch (IOException e) {
			System.out.println("3Datele nu au putut fi incarcate");
		}
		
	}

}
